<?php
/**
 * beimuaihui System
 * Copyright(c) 2011-2020 beimuaihui.
 * @license    http://www.gnu.org/licenses/gpl.html     This software Under GPL V3 License 
 * beimuaihui@gmail.com
 * http://code.google.com/p/beimuaihui/
 * $Id: DictController.php 197 2011-02-18 12:45:33Z beimuaihui $
 */



class Common_DictController extends Baogg_Controller_Action {

	function init() {		

	}

	
	/**
	 * The data action
	 */
	function dataAction() {
		global $LANG;
		
		
	    $post=$this->_request->getPost ();
	    
	    $dict_id=$post['dict_id'];
	    $query=$post['query'];
	    
	    
	   $where=array();$order=array();$limit=array();
	   foreach((array)$post as $k=>$v){
	       if($k=="start" || $k=="limit"){
	           $limit[$k]=$v;
	       }else if($k=="sort" || $k=="dir"){
	           $order[$k]=$v;
	       }else{
	           if($k=="dict_id"  ){
	               if($v && is_numeric($v)){
	                   $v=(int)$v;
	                   $where[$k]=$v;
	               }
	           }else{
	               $v='%'.$v.'%';
	               $where[$k]=$v;
	           }
	           
	       }
	   }
	   
	   $Dict=new Dict();
	   $data=$Dict->getDictList($dict_id,$where,$order,$limit);
	 /*   
	   $rs_dict=$Dict->getOne($dict_id);
	   $model_id=$rs_dict[0]['model_id'];
	   $where=$rs_dict[0]['filter']?array($rs_dict[0]['filter']):array();
	   $order=$rs_dict[0]['sorting']?array($rs_dict[0]['sorting']):array();
	   
	   
	   $Model=new Model();
	   $data=$Model->getComboData($model_id,$where,$order,$limit);    */
	   
	  
		$this->view->grid = Zend_Json::encode(array($data));
	}

	public function qastoreAction(){
		global $LANG;
	
	
		$post=$this->getRequest()->getParams();
	
		$qa_field_id=$post['qa_field_id'];
		$query=@$post['query'];
	
	
		$where=array();$order=array();$limit=array();
		foreach((array)$post as $k=>$v){
			if($k=="start" || $k=="limit"){
				$limit[$k]=$v;
			}else if($k=="sort" || $k=="dir"){
				$order[$k]=$v;
			}else{
				 if($v && $v!='#ALL#'){
					 $v='%'.$v.'%';
					 $where[$k]=$v; 
				 }
	
			}
		}
	
	
		$QaField=new QaField();
		$arr_text=$QaField->genFilterTextByFilterID($qa_field_id);	
		$rs_qa_field=$QaField->getOne($qa_field_id);
		
		if($query && $query!='#ALL#'){			
			$arr_query_text=array_filter($arr_text, create_function('$val', 'return strpos($val,"'.$query.'")!==false;'));	
			if($arr_query_text){
				$str_query_text=implode("','",array_keys($arr_query_text));
				$where[]="{$rs_qa_field[0]['qa_field_code']} like '%{$query}%' or {$rs_qa_field[0]['qa_field_code']} in ('{$str_query_text}')";
			}else{
				$where[$rs_qa_field[0]['qa_field_code']]=$query=='#ALL#'?'%%':'%'.$query.'%';
			}
		}
		
		$QaSurveys=new QaSurveys();
		$rs_qa_surveys=$QaSurveys->getOne($rs_qa_field[0]['qa_surveys_id']);
		$sid=$rs_qa_surveys[0]['sid'];
		//echo $sid;exit;
		$QaSurvey=new Qa_Survey($sid);
		
		//echo __FILE__.__LINE__.'<br /><pre>';print_r($where);exit;
		$where[]="submitdate is not null";
		$rs=$QaSurvey->getList($where,$order,$limit,array($rs_qa_field[0]['qa_field_code']),null,array($rs_qa_field[0]['qa_field_code']));
		/* if(count($rs)==1){
			unset($where[$rs_qa_field[0]['qa_field_code']]);
			$rs=array_merge($rs,$QaSurvey->getList($where,$order,$limit,array($rs_qa_field[0]['qa_field_code']),null,array($rs_qa_field[0]['qa_field_code'])));
		} */
		$arr=array(array($rs_qa_field[0]['qa_field_code']=>'#ALL#','text'=>Baogg_Language::get('select_all')));		
		foreach((array)$rs as $v){
			$field=$rs_qa_field[0]['qa_field_code'];
			$v['text']= isset($arr_text[ $v[$field]])?$arr_text[ $v[$field]]:$v[$field];
			$arr[]=$v;
		}
		
		
		$rs_cnt=$QaSurvey->getList($where,null,null,array('cnt'=>new Zend_Db_Expr('1')),null,array($rs_qa_field[0]['qa_field_code']));
	
		/*
		 $rs_dict=$Dict->getOne($qa_field_id);
		$model_id=$rs_dict[0]['model_id'];
		$where=$rs_dict[0]['filter']?array($rs_dict[0]['filter']):array();
		$order=$rs_dict[0]['sorting']?array($rs_dict[0]['sorting']):array();
	
	
		$Model=new Model();
		$data=$Model->getComboData($model_id,$where,$order,$limit);    */
	
	
		echo Zend_Json::encode ( array ("count" => count($rs_cnt ), "data" => $arr ) );
		$this->_helper->viewRenderer->setNoRender ();
	
	}

}
